package com.zhongkai.service.impl;

import com.mysql.jdbc.StringUtils;
import com.zhongkai.Bean.PageBean;
import com.zhongkai.Bean.PillarBean;
import com.zhongkai.app.service.IBaseDao;
import com.zhongkai.app.service.PageQuerDao;
import com.zhongkai.app.service.SellQueryService;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.Sell;
import org.hibernate.criterion.*;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 销售查询
 */

@Service
@Transactional(rollbackFor = Exception.class)
public class SellQueryServiceImpl implements SellQueryService {

    @Autowired
    private IBaseDao iBaseDao;

    @Autowired
    private PageQuerDao pageQuerDao;


    @Override
    public ZhongkaiResult listSellQuery(Integer page, Integer rows) {
        if (page == 0 || rows == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }



        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Sell.class);
            detachedCriteria.add(Restrictions.eq("sellRemove","1")); //增加条件
            detachedCriteria.addOrder(Order.desc("sellTime")); //根据传入的字段进行降序排序
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }
    }


    /**
     * 销售记录查询
     *
     * @param page 页数
     * @param rows 每页数据条数
     * @param name 操作人员姓名
     * @param time 时间
     */
    @Override
    public ZhongkaiResult listQuery(Integer page, Integer rows,String name,Long time) {  //搜素

        if (page == 0 || rows == 0 ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        if (!StringUtils.isNullOrEmpty(name) || time != 0) {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Sell.class);
            if (!StringUtils.isNullOrEmpty(name)){
                detachedCriteria.add(Restrictions.eq("sellClient", name)); //增加条件
            }


            if (time != 0){
                DateTime now = new DateTime(time);
                long millis = now.dayOfWeek().roundCeilingCopy().getMillis();//明天00点的时间戳
                long floor = now.dayOfWeek().roundFloorCopy().getMillis();//今天00点的时间戳

                detachedCriteria.add(Restrictions.gt("sellTime", floor)); //增加条件
                detachedCriteria.add(Restrictions.lt("sellTime", millis)); //增加条件
            }

            detachedCriteria.addOrder(Order.desc("sellTime")); //根据传入的字段进行降序排序
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);

            return ZhongkaiResult.ok(pageBean);
        }else {
            return ZhongkaiResult.build(500,"请输入或选择检索参数");
        }

    }

    @Override
    public ZhongkaiResult analyze() {  //柱状
        DetachedCriteria detachedCriteria2 = DetachedCriteria.forClass(Sell.class);  //地区分布查询
        detachedCriteria2.add(Restrictions.eq("sellRemove", "1")); //增加条件
        // detachedCriteria2.add(Restrictions.eq("isPut", "2")); //增加条件 是否绑定业主  1 未入库 2 已入库
        List<Object[]> indName = getJhDate(detachedCriteria2, "sellName");
        List<PillarBean> pillarBeans = new ArrayList<>();
        for (Object[] objects : indName) {
            PillarBean pillarBean = new PillarBean();

            List<Sell> list = iBaseDao.find("from Sell pr where pr.sellName='" + objects[0] + "'");


            int count = 0;
            for (Sell indent : list) {

                count = count + Integer.valueOf(indent.getSellCount());
            }

            pillarBean.setName(objects[0].toString());

            pillarBean.setValue(count);

            pillarBeans.add(pillarBean);
            pillarBean = null;
        }

        return ZhongkaiResult.ok(pillarBeans);
    }

    @Override
    public ZhongkaiResult cake() {  //病状图
        DetachedCriteria detachedCriteria2 = DetachedCriteria.forClass(Sell.class);  //地区分布查询
        detachedCriteria2.add(Restrictions.eq("sellRemove", "1")); //增加条件
        // detachedCriteria2.add(Restrictions.eq("isPut", "2")); //增加条件 是否绑定业主  1 未入库 2 已入库
        //1待审批  2已审批 3 驳回
        //1未入库  2已入库

        List<Object[]> isOut = getJhDate(detachedCriteria2, "isOut");
        List<Object[]> sellStatus = getJhDate(detachedCriteria2, "sellStatus");
        List<PillarBean> pillarBeans = new ArrayList<>();

        for (Object[] objects : isOut) {
            PillarBean pillarBean = new PillarBean();
            if ("1".equals(objects[0].toString())) {
                pillarBean.setName("未出库");
                pillarBean.setValue(Integer.valueOf(objects[1].toString()));
            } else {
                pillarBean.setName("已出库");
                pillarBean.setValue(Integer.valueOf(objects[1].toString()));
            }

            pillarBeans.add(pillarBean);
            pillarBean = null;
        }


        for (Object[] objects : sellStatus) {
            PillarBean pillarBean = new PillarBean();
            if ("1".equals(objects[0].toString())) {
                pillarBean.setName("待审批");
                pillarBean.setValue(Integer.valueOf(objects[1].toString()));
            } else if ("2".equals(objects[0].toString())) {
                pillarBean.setName("已审批");
                pillarBean.setValue(Integer.valueOf(objects[1].toString()));
            } else if ("3".equals(objects[0].toString())) {
                pillarBean.setName("驳回");
                pillarBean.setValue(Integer.valueOf(objects[1].toString()));
            }
            pillarBeans.add(pillarBean);
            pillarBean = null;
        }

        return ZhongkaiResult.ok(pillarBeans);
    }



    public List<Object[]> getJhDate(DetachedCriteria detachedCriteria, String name) {
        ProjectionList projectionList = Projections.projectionList();


        projectionList.add(Projections.groupProperty(name));
        projectionList.add(Projections.rowCount());
        detachedCriteria.setProjection(projectionList);

        List<Object[]> list = pageQuerDao.pageMax2(detachedCriteria);

        return list;
    }
}
